package com.example.carrierapp.ui.main.waybill.arriveyes;

import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.RowsBean;
import com.example.carrierapp.bean.waybill.WaybillListBean;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.viewmodel.itemmodel.ItemWaybillModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class WaybillArriveYesPresenter extends BasePresenter<WaybillArriveYesContract.View> implements WaybillArriveYesContract.Presenter {
    private Context mContext;

    public WaybillArriveYesPresenter(Context context) {
        mContext = context;
    }

    @Override
    public void start() {

    }

    @Override
    public void getWaybillList(int skipCount, String filter) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("billState", "3");
        biddingMap.put("maxResultCount", "10");
        biddingMap.put("skipCount", skipCount + "");
        biddingMap.put("filter", filter);
        Flowable<JsonObject> flowable = apiStrores.getWaybillList(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("getWaybillList-onSuccess-" + json);

                RowsBean<ItemWaybillModel> bean = new Gson().fromJson(json, new TypeToken<RowsBean<ItemWaybillModel>>() {
                }.getType());
                if (getView() != null) {
                    getView().getWaybillListSuccess(bean);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getWaybillList-onFailure-" + msg);
                if (getView() != null) {
                    getView().getWaybillListFailde(msg);
                }
            }
        });
    }

    @Override
    public void modifyVolume(int id, String volume, String carNum,boolean isConfirmed) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("carNum", carNum + "");
        biddingMap.put("id", id + "");
        biddingMap.put("unloadingNum", volume + "");
        biddingMap.put("isConfirmed", isConfirmed + "");
        Flowable<JsonObject> flowable = apiStrores.modifyCarnumOrVolume(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("modifyVolume-onSuccess-" + json);
                if (getView() != null) {
                    getView().modifyVolumeSuccess(json);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("modifyVolume-onFailure-" + msg);
                if (getView() != null) {
                    getView().modifyVolumeFailde(msg);
                }
            }
        });
    }
}
